package com.astrob.model;

import android.content.Context;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;

import com.astrob.naviframe.Start;
import com.astrob.nhcmap.R;

import java.util.ArrayList;

public class FavoriteFileHandle {
	private static String FAVPATH = Start.RUNDIR + "/fav.dat";
	private static FavoriteFileHandle instance = new FavoriteFileHandle();
	private ArrayList<FavoriteData> mListFavorite = new ArrayList<FavoriteData>();
	public FavoriteData editFav;

	public static FavoriteFileHandle getInstance() {
		return instance;
	}

	public ArrayList<FavoriteData> getListFavorite() {
		return mListFavorite;
	}

	public FavoriteFileHandle() {
		load();
	}

	private boolean mhasBind = false;

	public void setHasBind(boolean hasBind) {
		mhasBind = hasBind;
	}

	public boolean hasBind() {
		return mhasBind;
	}

	public int getFavIconID(int index) {
		return R.drawable.poitonmapicon05;
	}

	public String getFavSoundID(Context context, String url) {
		final Ringtone ringtone = RingtoneManager.getRingtone(context,
				Uri.parse(url));
		if (ringtone == null) {
			return "";
		}
		return ringtone.getTitle(context);
	}

	public void load() {
		mListFavorite.clear();
		ReadObjectsFromFile<FavoriteData> readFile = new ReadObjectsFromFile<FavoriteData>();
		readFile.onReadObjectsList(FAVPATH, mListFavorite);
	}

	@SuppressWarnings("unchecked")
	public void save() {
		@SuppressWarnings("rawtypes")
		WriteObjectsToFile writeFile = new WriteObjectsToFile();
		writeFile.onWriteObjects(FAVPATH, mListFavorite);
	}
	private FavoriteData Hist2Fav(HistoryData data){
		FavoriteData fav = new FavoriteData();
		fav.revert();
		fav.eid = data.eid;
		fav.setLl(data.getLl());
		fav.setName(data.getName());
		fav.setAddress(data.getAddress());
		fav.setPhoneNum(data.getPhoneNum());
		fav.countryID = data.countryID;
		fav.pcode = data.pcode;
		fav.setCurrentTimeMillis(System.currentTimeMillis());
		return fav;
	}
	public boolean addByHist(HistoryData data){
		FavoriteData fav = Hist2Fav(data);
		if(haveSameFav(fav)){
			return true;
		}
		mListFavorite.add(fav);
		save();
		return true;		
	}
	public boolean deleteByHist(HistoryData data){
		FavoriteData fav = Hist2Fav(data);
		if(haveSameFav(fav)){
			delete(fav);
			save();
			return true;
		}
		return false;	
	}
	public boolean isHistInFav(HistoryData data){
		FavoriteData fav = Hist2Fav(data);
		if(haveSameFav(fav)){
			return true;
		}
		return false;
	}
	
	public boolean addFromPhote(FavoriteData fav) {
		if (fav == null || haveSameFav(fav)) {
			return false;
		}

		mListFavorite.add(fav);
		return true;
	}
	
	public boolean add(FavoriteData fav) {
		fav.countryID = "CN";
		
		if (fav == null || haveSameFav(fav)) {
			int index = mListFavorite.indexOf(fav);
			// if have named delete, name it as edit
			if (mListFavorite.get(index).action == 3) {
				mListFavorite.get(index).action = 2;
			}
			return false;
		}
		
		// must insert before deleted items
		int size = mListFavorite.size();
		int i = size - 1;
		for (; i >= 2; --i) { // home and company not include
			if (!mListFavorite.get(i).isEclipse()) {
				break;
			}
		}
		i = i + 1;// add after find index
		fav.setCurrentTimeMillis(System.currentTimeMillis());
		if (i == size) { // if no eclipse item
			mListFavorite.add(fav);
			save();
			return true;
		} else {
			mListFavorite.add(i, fav);
			save();
			return true;
		}

	}

	public void delete(int index) {
		if (index >= 0 && index < mListFavorite.size()) {
			FavoriteData fav = mListFavorite.get(index);
			mListFavorite.remove(index);
			// if not commit to add, delete it
			if (fav.action != 1) {
				fav.action = 3;
				mListFavorite.add(fav);
			}
		}
	}
	public void delete(FavoriteData fav) {
		int index = mListFavorite.indexOf(fav);
		if (index >= 0 && index < mListFavorite.size()) {
			FavoriteData f = mListFavorite.get(index);
			mListFavorite.remove(index);
			// if not commit to add, delete it
			if (f.action != 1) {
				f.action = 3;
				mListFavorite.add(f);
			}
		}
	}

	public boolean haveSameFav(FavoriteData fav) {
		if (fav == null || mListFavorite.size() < 1) {
			return false;
		}
		return mListFavorite.contains(fav);
	}

	public boolean isFavPosValid(FavoriteData fav) {
		LonLat ll = fav.getLl();
		if (ll.GetLon() > 0 && ll.GetLon() < 180 && ll.GetLat() > 0
				&& ll.GetLat() < 90) {
			return true;
		}
		return false;
	}

	public void removeAll() {
		// mListFavorite.clear();		
		ArrayList<FavoriteData> listFavorite = new ArrayList<FavoriteData>();
		for (FavoriteData data : mListFavorite) {
			listFavorite.add(data);
		}
		
		for (int i = 0; i < listFavorite.size(); i++) {
			FavoriteData data = listFavorite.get(i);
			if (data.countryID.compareToIgnoreCase("CN") == 0) {
				mListFavorite.remove(data);
			}
		}
		
		save();
	}
}